package com.doradance.app.home;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.doradance.R;
import com.doradance.entity.VipEntity;

import java.util.List;

/**
 * Created by Administrator on 2018/3/5.
 */


public class vipAdapter extends BaseAdapter {
    LayoutInflater mInflater;
    final List<VipEntity.VipsBean> vips1;
    Boolean isOpen1;
    Boolean isHighest1;
    private Context mContext;

    public vipAdapter(Context context, List<VipEntity.VipsBean> mList,Boolean isOpen,Boolean ishighest) {
        vips1 = mList;
        isHighest1=ishighest;
        isOpen1 =isOpen;
        mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return vips1.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.vip_recharge_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.tvVipNameItem = (TextView) convertView.findViewById(R.id.tv_vip_name_item);
            viewHolder.btnRechargeItem = (Button) convertView.findViewById(R.id.btn_recharge_item);
            viewHolder.tvVipPrice = (TextView) convertView.findViewById(R.id.tv_vip_price);
            viewHolder.tvVipDays = (TextView) convertView.findViewById(R.id.tv_vip_days);
            viewHolder.ivVipCover = (ImageView) convertView.findViewById(R.id.iv_vip_cover);
            viewHolder.ivVipLogo = (ImageView) convertView.findViewById(R.id.iv_vip_logo);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvVipNameItem.setText(vips1.get(position).getName()+"VIP");
        viewHolder.tvVipPrice.setText("¥" + vips1.get(position).getPrice());
        viewHolder.tvVipDays.setText(vips1.get(position).getDays() + "天");
        if (isOpen1 == false) {
            viewHolder.btnRechargeItem.setText("开通");
        } else if (isOpen1 == true && isHighest1 == true) {
            viewHolder.btnRechargeItem.setText("已开通");
        } else {
            viewHolder.btnRechargeItem.setText("升级");
        }

        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(45);
        drawable.setColor(Color.parseColor("#FF85A2"));
        viewHolder.btnRechargeItem.setBackground(drawable);
        if(position==0){
            Glide.with(mContext).load(R.mipmap.icon_card1).asBitmap().into(viewHolder.ivVipCover);
            drawable.setColor(Color.parseColor("#FF85A2"));
            viewHolder.btnRechargeItem.setBackground(drawable);
            Glide.with(mContext).load(vips1.get(0).getIcon()).asBitmap().into(viewHolder.ivVipLogo);

        }else if (position == 1){
            Glide.with(mContext).load(R.mipmap.icon_card2).asBitmap().into(viewHolder.ivVipCover);
            drawable.setColor(Color.parseColor("#43C6DC"));
            viewHolder.btnRechargeItem.setBackground(drawable);
            Glide.with(mContext).load(vips1.get(1).getIcon()).asBitmap().into(viewHolder.ivVipLogo);
        }else if(position == 2){
            Glide.with(mContext).load(R.mipmap.icon_card3).asBitmap().into(viewHolder.ivVipCover);
            drawable.setColor(Color.parseColor("#64A1EB"));
            viewHolder.btnRechargeItem.setBackground(drawable);
            Glide.with(mContext).load(vips1.get(2).getIcon()).asBitmap().into(viewHolder.ivVipLogo);
        }
        else {
            Glide.with(mContext).load(R.mipmap.icon_card3).asBitmap().into(viewHolder.ivVipCover);
            drawable.setColor(Color.parseColor("#64A1EB"));
            viewHolder.btnRechargeItem.setBackground(drawable);
            Glide.with(mContext).load(R.mipmap.icon_grade4).asBitmap().into(viewHolder.ivVipLogo);
        }

        viewHolder.btnRechargeItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemOpenListener.onOpenClick(position);
            }
        });
        return convertView;
    }

    private onItemOpenListener mOnItemOpenListener;

    public interface onItemOpenListener {
        void onOpenClick(int position);
    }

    public void setOnItemOpenListener(onItemOpenListener onItemOpenListener) {
        mOnItemOpenListener = onItemOpenListener;
    }
    class ViewHolder {
        TextView tvVipPrice;
        TextView tvVipNameItem;
        Button btnRechargeItem;
        TextView tvVipDays;
        ImageView ivVipCover;
        ImageView ivVipLogo;
    }

}
